<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$MTF = JFX::registry('ThirdParty_MTF');
$BWT = JFX::registry('ThirdParty_BWT');

$keyname = $DB->escape(get('keyname'));
$version = $DB->escape(get('version'));

$packageid = $DB->oneValue($CONFIG->dbprefix.'vms_packages', 'id', "keyname = '{$keyname}'");
$vData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_versions WHERE package_id = '{$packageid}' AND version = '{$version}'");

$q = "SELECT COUNT(*) FROM {$CONFIG->dbprefix}vms_versions_to_groups v JOIN {$CONFIG->dbprefix}iusers_to_groups i2g ON (v.group_key = i2g.group_key)
    WHERE i2g.user_id = '{$IUSER->userid}'";

$row = $DB->fetchRow($q);

if($row['COUNT(*)']==0){
    // not allowed this file
    $res->result = 'FAIL';
    $res->reason = 'Insufficient privileges';
    die(serialize($res));
}

if(is_array($vData) && count($vData)>0){
    $res->result = 'SUCCESS';

    $files = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_files WHERE package_id = '{$packageid}' AND version_id = '{$vData['id']}'");

    foreach($files as $k=>$v){
//        $files[$k]['file_contents'] = $MTF->encode($BWT->transform(file_get_contents($this->dirname.'/files/'.$v['id'].'.vmsf')));
          //$files[$k]['file_contents'] = file_get_contents($this->dirname.'/files/'.$v['id'].'.vmsf');
          $files[$k]['file_contents'] = $FCRYPT->crypt(file_get_contents($this->dirname.'/files/'.$v['id'].'.vmsf'));
    }

    $res->files = $files;

}else{
    $res->result = 'FAIL';
    $res->reason = 'Could not find version data';
}

die(serialize($res));

